package edu.hebtu.controller;

import edu.hebtu.mapper.impl.AdminMapper;
import edu.hebtu.mapper.impl.UserMapper;
import edu.hebtu.mapper.impl.SubjectMapper;
import edu.hebtu.pojo.Admin;
import edu.hebtu.pojo.Subject;
import edu.hebtu.pojo.User;
import edu.hebtu.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

@Controller
public class UserController {

    private final UserMapper userMapper;
    private final SubjectMapper subjectMapper;
    private final AdminMapper adminMapper;

    @Autowired
    public UserController(UserMapper userMapper, SubjectMapper subjectMapper, AdminMapper adminMapper) {
        this.userMapper = userMapper;
        this.subjectMapper = subjectMapper;
        this.adminMapper = adminMapper;
    }

    @RequestMapping("/user_info")
    public String select_user(Model model){
        List<User> users = userMapper.findById(null);
        users.removeIf(aValue -> aValue.getUserType() == 1);
        for (User user : users) {
            List<Admin> admins = adminMapper.findById(user.getCreator());
            if (admins.size() > 0) {
                user.setCreator(admins.get(0).getName());
            }
            if (user.getState().trim().equals("appear")) user.setState("正常");
            if (user.getState().trim().equals("licenced")) user.setState("已拿证");
        }
        model.addAttribute("users", users);
        return "tables/table-user";
    }

    @RequestMapping("/trainer_info")
    public String select_trainer(Model model) {
        List<User> users = userMapper.findById(null);
        users.removeIf(aValue -> aValue.getUserType() == 0);
        for (User user : users) {
            List<Subject> subjects = subjectMapper.findById(user.getSubjectId().trim());
            List<Admin> admins = adminMapper.findById(user.getCreator());
            if (subjects.size() == 1) {
                user.setSubjectId(subjects.get(0).getSubjectName());
            }
            if (admins.size() == 1) {
                user.setCreator(admins.get(0).getName());
            }
            if (user.getState().trim().equals("appear")) user.setState("正常");
        }
        model.addAttribute("users", users);
        return "tables/table-trainer";
    }

    private void init_Model(Model model){
        List<Subject> subjects = subjectMapper.findById(null);
        subjects.remove(0);
        subjects.remove(subjects.size() - 1);
        model.addAttribute("subjects", subjects);
    }

    @RequestMapping("/trainer_add")
    public String trainer_add(Model model) {
        init_Model(model);
        List<Subject> subjects = subjectMapper.findById(null);
        model.addAttribute("sex", 1);
        model.addAttribute("parent", "C1");
        model.addAttribute("subjectId", subjects.get(1).getSubjectId());
        return "add/add-trainer";
    }

    @RequestMapping("/user_add")
    public String user_add(Model model) {
        model.addAttribute("sex", 1);
        model.addAttribute("parent", "C1");
        return "add/add-user";
    }

    @RequestMapping("/addUser")
    public String addTrainer(User user, Model model) {

        if (user.getUserType() == 1) init_Model(model);

        List<User> users = userMapper.findById(null);
        boolean flag = false;
        if (user.getPhone() == null || user.getPhone().trim().equals("")) {
            model.addAttribute("msg", "请输入手机号！！！");
            flag = true;
        } else if (!PhoneCheck.check(user.getPhone().trim())) {
            model.addAttribute("msg", "手机号格式有误，请重新输入！！！");
            flag = true;
        } else {
            for (User user1 : users) {
                if (user.getPhone().trim().equals(user1.getPhone().trim())) {
                    model.addAttribute("msg", "手机号已存在！！！");
                    flag = true;
                }
            }
        }
        if (user.getCard() == null || user.getCard().trim().equals("")) {
            model.addAttribute("msg", "请输入身份证号！！！");
            flag = true;
        } else if (!IDCheck.check(user.getCard())) {
            model.addAttribute("msg", "身份证号格式有误，请重新输入！！！");
            flag = true;
        } else {
            for (User user1 : users) {
                if (user.getCard().trim().equals(user1.getCard().trim())) {
                    model.addAttribute("msg", "身份证号已存在！！！");
                    flag = true;
                }
            }
        }
        if (user.getName() == null || user.getName().trim().equals("")) {
            model.addAttribute("msg", "请输入姓名！！！");
            flag = true;
        }

        if (flag) {
            model.addAttribute("name", user.getName());
            model.addAttribute("card",user.getCard());
            model.addAttribute("phone",user.getPhone());
            model.addAttribute("parent", user.getParent());
            model.addAttribute("subjectId", user.getSubjectId());
            if (user.getUserType() == 1) return "add/add-trainer";
            else return "add/add-user";
        }

        user.setPassword("123456");
        user.setCreateDate(new Date());
        if (userMapper.add(user) == 1) {
            model.addAttribute("sex", 1);
            model.addAttribute("parent", "C1");
            model.addAttribute("msg", "添加成功，" + user.getName() + "的账号为：" + user.getUserId() + "，密码为：123456。");
        } else {
            model.addAttribute("msg", "添加失败！！！");
        }
        if (user.getUserType() == 1) return "add/add-trainer";
        else return "add/add-user";
    }

    @RequestMapping("/user_edit/{userId}")
    public String user_edit(@PathVariable("userId")String userId, Model model) {
        User user = userMapper.findById(userId).get(0);
        List<Subject> subjects = subjectMapper.findById(null);
        subjects.remove(0);
        subjects.remove(subjects.size() - 1);
        model.addAttribute("user", user);
        model.addAttribute("subjects", subjects);
        return "edit/edit-user";
    }

    @RequestMapping("/editUser")
    public String editAdmin(User user, Model model) {

        if (user.getUserType() == 1) init_Model(model);

        List<User> users = userMapper.findById(null);
        users.removeIf(aValue -> aValue.getUserId().trim().equals(user.getUserId()));
        boolean flag = false;
        if (user.getPhone() == null || user.getPhone().trim().equals("")) {
            model.addAttribute("msg", "请输入手机号！！！");
            flag = true;
        } else if (!PhoneCheck.check(user.getPhone().trim())) {
            model.addAttribute("msg", "手机号格式有误，请重新输入！！！");
            flag = true;
        } else {
            for (User user1 : users) {
                if (user.getPhone().trim().equals(user1.getPhone().trim())) {
                    model.addAttribute("msg", "手机号已存在！！！");
                    flag = true;
                }
            }
        }
        if (user.getCard() == null || user.getCard().trim().equals("")) {
            model.addAttribute("msg", "请输入身份证号！！！");
            flag = true;
        } else if (!IDCheck.check(user.getCard())) {
            model.addAttribute("msg", "身份证号格式有误，请重新输入！！！");
            flag = true;
        } else {
            for (User user1 : users) {
                if (user.getCard().trim().equals(user1.getCard().trim())) {
                    model.addAttribute("msg", "身份证号已存在！！！");
                    flag = true;
                }
            }
        }
        if (user.getName() == null || user.getName().trim().equals("")) {
            model.addAttribute("msg", "请输入姓名！！！");
            flag = true;
        }

        if (flag) {
            model.addAttribute("user", user);
            return "edit/edit-user";
        }

        if (userMapper.update(user) == 1) {
            if (user.getUserType() == 1) return "redirect:/trainer_info";
            else return "redirect:/user_info";
        } else {
            model.addAttribute("msg", "修改失败！！！");
            return "edit/edit-user";
        }

    }

    @RequestMapping("/user_delete/{userId}")
    public String user_delete(@PathVariable("userId")String userId, Model model) {
        User user = userMapper.findById(userId).get(0);
        List<Subject> subjects = subjectMapper.findById(null);
        subjects.remove(0);
        subjects.remove(subjects.size() - 1);
        model.addAttribute("user", user);
        model.addAttribute("subjects", subjects);
        return "delete/delete-user";
    }

    @RequestMapping("/deleteUser")
    public String deleteCar(User user) {
        userMapper.delete(user);
        if (user.getUserType() == 1) return "redirect:/trainer_info";
        else return "redirect:/user_info";
    }

}
